package cn.iocoder.yudao.module.project.controller.admin.productprocesscompleted;

import cn.hutool.core.date.DateUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.productprocesscompleted.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.productprocesscompleted.ProductProcessCompletedDO;
import cn.iocoder.yudao.module.project.service.productprocesscompleted.ProductProcessCompletedService;

@Tag(name = "管理后台 - 产品完成工序")
@RestController
@RequestMapping("/project/product-process-completed")
@Validated
public class ProductProcessCompletedController {

    @Resource
    private ProductProcessCompletedService productProcessCompletedService;

    @PostMapping("/create")
    @Operation(summary = "创建产品完成工序")
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:create')")
    public CommonResult<Integer> createProductProcessCompleted(@Valid @RequestBody ProductProcessCompletedSaveReqVO createReqVO) {
        return success(productProcessCompletedService.createProductProcessCompleted(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品完成工序")
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:update')")
    public CommonResult<Boolean> updateProductProcessCompleted(@Valid @RequestBody ProductProcessCompletedSaveReqVO updateReqVO) {
        productProcessCompletedService.updateProductProcessCompleted(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品完成工序")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:delete')")
    public CommonResult<Boolean> deleteProductProcessCompleted(@RequestParam("id") Integer id) {
        productProcessCompletedService.deleteProductProcessCompleted(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品完成工序")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:query')")
    public CommonResult<ProductProcessCompletedRespVO> getProductProcessCompleted(@RequestParam("id") Integer id) {
        ProductProcessCompletedDO productProcessCompleted = productProcessCompletedService.getProductProcessCompleted(id);
        return success(BeanUtils.toBean(productProcessCompleted, ProductProcessCompletedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品完成工序分页")
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:query')")
    public CommonResult<PageResult<ProductProcessCompletedRespVO>> getProductProcessCompletedPage(@Valid ProductProcessCompletedPageReqVO pageReqVO) {
        PageResult<ProductProcessCompletedDO> pageResult = productProcessCompletedService.getProductProcessCompletedPage(pageReqVO);
        PageResult<ProductProcessCompletedRespVO> voPageResult = BeanUtils.toBean(pageResult, ProductProcessCompletedRespVO.class);

        List<ProductProcessCompletedRespVO> list = voPageResult.getList();
        for (ProductProcessCompletedRespVO productProcessCompletedRespVO : list){
            LocalDateTime localDateTime = productProcessCompletedRespVO.getLastCollectionTime();
            if (localDateTime != null) {
                String localDateTimeStr = DateUtil.format(localDateTime, "yyyy-MM-dd HH:mm:ss");
                productProcessCompletedRespVO.setLastCollectionTimeStr(localDateTimeStr);
            }
        }

        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品完成工序 Excel")
    //@PreAuthorize("@ss.hasPermission('project:product-process-completed:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductProcessCompletedExcel(@Valid ProductProcessCompletedPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductProcessCompletedDO> list = productProcessCompletedService.getProductProcessCompletedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品完成工序.xls", "数据", ProductProcessCompletedRespVO.class,
                        BeanUtils.toBean(list, ProductProcessCompletedRespVO.class));
    }

}